package CustomClassFile.CronJob;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.wxinterface.InterfaceUtil;

/**
 * 实现Job接口，定义运行的任务
 * 
 * @author Ickes
 */
public class MyJob implements Job {

	public void execute(JobExecutionContext context) throws JobExecutionException {
		 JobDataMap jdMap=context.getJobDetail().getJobDataMap();
		List<String> userlist=(List<String>) jdMap.get("userlist");
		String contentval=String.valueOf(jdMap.get("content"));
		String card=String.valueOf(jdMap.get("card"));//打卡班次
		String date=String.valueOf(jdMap.get("date"));//日期
		BaseBean writelog = new BaseBean();
		String jobname=context.getJobDetail().getName();
		writelog.writeLog(jobname+"|定时任务开始执行");
		writelog.writeLog(jobname+"|推送云桥消息开始");
		writelog.writeLog(jobname+"|content:"+contentval);
		writelog.writeLog(jobname+"|-----------userid:"+userlist);
		List<String> flowuserid=selectbeforeDK(card, date);
		writelog.writeLog(jobname+"|已打卡的----userid:"+flowuserid);
		List<String> ids=new ArrayList<String>();
		for (String strid : userlist) {
			if (!flowuserid.contains(strid)) {
				ids.add(strid);
			}
		}
		writelog.writeLog(jobname+"|移除已打卡的userid:"+ids);
		try {
			String tpids = "a2d0b0a7e24b4fbd8455fae1452d15cf";

			String dataid = "";

			String content =contentval;

			int type = 16;

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("msgurl", "/mobilemode/appHomepageViewWrap.jsp?appHomepageId=15");

			map.put("msgPcUrl", "/mobilemode/appHomepageViewWrap.jsp?appHomepageId=15");

			map.put("imgurl", "http://pic31.photophoto.cn/20140413/0049045698345890_b.jpg");

			Map<String, String> result = InterfaceUtil.sendMsg(ids, tpids, dataid, content, type, map);
			if (result != null) {
				String msgcode = (String) result.get("msgcode");

				String msginfo = (String) result.get("msginfo");
				writelog.writeLog(jobname+"|msgcode====" + msgcode);
				writelog.writeLog(jobname+"|msginfo====" + msginfo);
			}
			writelog.writeLog(jobname+"推送云桥消息结束");
		} catch (Exception e) {
			e.printStackTrace();
			writelog.writeLog(jobname+"|消息推送失败，程序异常:" + e.getMessage());
		}
	}
	
	//查询当天班次已经打卡的人员，用来做为已经打卡的人排除，不再发送打卡提醒
	public List<String> selectbeforeDK(String cardname,String date){
		List<String> userid=new ArrayList<>();
		RecordSet rs = new RecordSet();
		String sql="select kqry from uf_kqxx where dkcard=? and pbrq=?";
		rs.executeQuery(sql, cardname,date);
		while(rs.next()){
			userid.add(rs.getString("kqry"));
		}
		return userid;
	}
}
